package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.GrantStockVsPrescription;
import com.zp.drug.entity.bo.GrantStockVsPrescriptionBo;
import com.zp.drug.entity.vo.GrantStockVsPrescriptionVo;
import com.zp.drug.mapper.GrantStockVsPrescriptionMapper;
import com.zp.drug.service.IGrantStockVsPrescriptionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 保存库存扣减、stock_log和处方对应Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-06-07
 */
@RequiredArgsConstructor
@Service
public class GrantStockVsPrescriptionServiceImpl implements IGrantStockVsPrescriptionService {

    private final GrantStockVsPrescriptionMapper baseMapper;

    /**
     * 查询保存库存扣减、stock_log和处方对应
     */
    @Override
    public GrantStockVsPrescriptionVo queryById(String grantStockVsId) {
        return baseMapper.selectVoById(grantStockVsId);
    }

    /**
     * 查询保存库存扣减、stock_log和处方对应列表(分页)
     */
    @Override
    public TableDataInfo<GrantStockVsPrescriptionVo> queryPageList(GrantStockVsPrescriptionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GrantStockVsPrescription> lqw = buildQueryWrapper(bo);
        Page<GrantStockVsPrescriptionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询保存库存扣减、stock_log和处方对应列表(不分页)
     */
    @Override
    public List<GrantStockVsPrescriptionVo> queryList(GrantStockVsPrescriptionBo bo) {
        LambdaQueryWrapper<GrantStockVsPrescription> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GrantStockVsPrescription> buildQueryWrapper(GrantStockVsPrescriptionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GrantStockVsPrescription> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStockId()), GrantStockVsPrescription::getStockId, bo.getStockId());
        lqw.eq(StringUtils.isNotBlank(bo.getIdenticalInfoId()), GrantStockVsPrescription::getIdenticalInfoId, bo.getIdenticalInfoId());
        lqw.eq(bo.getNum() != null, GrantStockVsPrescription::getNum, bo.getNum());
        lqw.eq(StringUtils.isNotBlank(bo.getPrescriptionApplyId()), GrantStockVsPrescription::getPrescriptionApplyId, bo.getPrescriptionApplyId());
        lqw.eq(StringUtils.isNotBlank(bo.getPrescriptionItemId()), GrantStockVsPrescription::getPrescriptionItemId, bo.getPrescriptionItemId());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNo()), GrantStockVsPrescription::getBatchNo, bo.getBatchNo());
        return lqw;
    }

    /**
     * 新增保存库存扣减、stock_log和处方对应
     */
    @Override
    public Boolean insertByBo(GrantStockVsPrescriptionBo bo) {
        GrantStockVsPrescription add = BeanUtil.toBean(bo, GrantStockVsPrescription.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGrantStockVsId(add.getGrantStockVsId());
        }
        return flag;
    }

    /**
     * 修改保存库存扣减、stock_log和处方对应
     */
    @Override
    public Boolean updateByBo(GrantStockVsPrescriptionBo bo) {
        GrantStockVsPrescription update = BeanUtil.toBean(bo, GrantStockVsPrescription.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GrantStockVsPrescription entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除保存库存扣减、stock_log和处方对应
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 组织并保存数据
     *
     * @param stockId
     * @param identicalInfoId
     * @param num
     * @param prescriptionApplyId
     * @param prescriptionItemId
     * @param batchNo
     * @return
     */
    @Override
    public Boolean tissueAndSaveInfo(String stockId, String identicalInfoId, Integer num,
                                     String prescriptionApplyId, String prescriptionItemId,
                                     String batchNo, String drugStockAllLogId) {
        GrantStockVsPrescriptionBo bo = new GrantStockVsPrescriptionBo();
        bo.setBatchNo(batchNo);
        bo.setStockId(stockId);
        bo.setIdenticalInfoId(identicalInfoId);
        bo.setNum(num);
        bo.setPrescriptionApplyId(prescriptionApplyId);
        bo.setPrescriptionItemId(prescriptionItemId);
        bo.setDrugStockAllLogId(drugStockAllLogId);
        return insertByBo(bo);
    }
}
